#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>

#define TAB44 "    "
#define TAB46 "      "
#define PRINTF_DEBUG

#define TS_PAT_PID 0x0

#define TS_PAT_TABLE_PID 0x0
#define TS_PMT_TABLE_PID 0x2

#define MAX_PDTS_LEN 5
#define MAX_TS_PROGRAM_NUM 8
#define MAX_TS_STREAM_NUM 8
#define MAX_PDTS_STRING_LEN 12
#define MAX_TS_PACKET_LEN 188 /* 204, 188+16字节的CRC */


/******************************************************************
视频
MPEG-1 Video：stream_type为0x01
MPEG-2 Video：stream_type为0x02
AVC（H264）：stream_type为0x1b
VC-1：stream_type为0xea

音频
Mpeg-1 Audio：stream_type为0x03
Mpeg-2 Audio：stream_type为0x04
Mpeg-2 AAC：stream_type为0x0f
Mpeg-4 AAC：stream_type为0x11
LPCM：stream_type为0x80
AC3：stream_type为0x81或0x06
DTS：stream_type为0x82
Dolby TrueHD：stream_type为0x83
AC3-Plus：stream_type为0x84
DTS_HD：stream_type为0x85
DTS-MA：stream_type为0x86
AC3-Plus_SEC：steam_type为0xa1
DTS_HD_SEC：stream_type为0xa2

字幕
PGS：stream_type为0x90
IGS：steam_type为0x91，暂不支持
Text Subtitle：stream_type为0x92
********************************************************************/
typedef enum t_ts_stream_type
{
    E_STREAM_TYPE_MPEG1_VIDEO         = 0x01,
    E_STREAM_TYPE_MPEG2_VIDEO         = 0x02,
    E_STREAM_TYPE_AVC_VIDEO         = 0x1B,
    E_STREAM_TYPE_VC1_VIDEO         = 0xEA,
    E_STREAM_TYPE_MPEG1_AUDIO         = 0x03,
    E_STREAM_TYPE_MPEG2_AUDIO         = 0x04,
    E_STREAM_TYPE_MPEG2_AAC         = 0x0F,
    E_STREAM_TYPE_MPEG4_AAC         = 0x11,
    E_STREAM_TYPE_AC3                 = 0x81,
} T_TS_STREAM_TYPE;

/* 4 bytes */
typedef struct t_ts_packet_header
{
    unsigned char sync_byte;
    unsigned short transport_error_indictor:1, playload_unit_start_indictor:1, transport_priority:1, pid:13;
    unsigned char transport_scrambling_control:2, adaptation_field_control:2, continuity_counter:4;
} T_TS_PACKET_HEADER;

/* PAT */
typedef struct t_ts_program
{
    unsigned short program_number;
    unsigned short program_map_pid;
} T_TS_PROGRAM;

typedef struct t_ts_pat
{
    unsigned char table_id;
    unsigned short section_len;
    unsigned char version_num:5;
    unsigned short programNum;
    
    T_TS_PROGRAM programs[MAX_TS_PROGRAM_NUM];
} T_TS_PAT;

/* PMT */
typedef struct t_ts_stream
{
    unsigned char stream_type;
    unsigned short elementary_pid;
} T_TS_STREAM;

typedef struct t_ts_pmt
{
    unsigned short pmtIsFind;
    unsigned char table_id;
    unsigned short section_len;
    unsigned short program_number;
    unsigned char version_num:5;
    unsigned short program_info_length;
    
    unsigned short streamNum;
    
    T_TS_STREAM streams[MAX_TS_STREAM_NUM];
} T_TS_PMT;

/* PES */
typedef struct t_ts_pes
{
    unsigned char streamId;
    
    unsigned short pesLength;
    
    long long pts;
    long long dts;
    
    unsigned char ptsStr[MAX_PDTS_STRING_LEN+1];
    unsigned char dtsStr[MAX_PDTS_STRING_LEN+1];
    
    unsigned char pesHeaderLen;
} T_TS_PES;

T_TS_PAT g_TsPat = {0};
T_TS_PMT g_TsPmt[MAX_TS_PROGRAM_NUM] = {0};

static void ParseTsHeader(unsigned char* const headerData, T_TS_PACKET_HEADER *tsPacketHeader)
{
    static int tsPacketNum = 0;
    
    int offset = 0;
    
    unsigned char *data = NULL;
    
    T_TS_PACKET_HEADER tsHeader = {0};
    
    memset(&tsHeader, 0x0, sizeof(tsHeader));
    
    data = headerData;
    
    tsHeader.sync_byte = data[0];
    tsHeader.transport_error_indictor = ((data[1]>>7)&0x1);
    tsHeader.playload_unit_start_indictor = ((data[1]>>6)&0x1);
    tsHeader.transport_priority = ((data[1]>>5)&0x1);
    tsHeader.pid = (((data[1]&0x1f)<<8) | data[2]);
    tsHeader.transport_scrambling_control = ((data[3]>>6)&0x3);
    tsHeader.adaptation_field_control = ((data[3]>>4)&0x3);
    tsHeader.continuity_counter = data[3]&0xf;
    
    memcpy(tsPacketHeader, &tsHeader, sizeof(tsHeader));
    
#ifdef PRINTF_DEBUG
    offset = tsPacketNum*MAX_TS_PACKET_LEN;
    
    switch (tsHeader.adaptation_field_control)
    {        
        case 1:
            if (tsHeader.playload_unit_start_indictor)
            {
                printf("0x%08x Transport Packet{PID = 0x%x, Payload = YES(%d), Counter = %d, Start indicactor}\n", 
                        offset, tsHeader.pid, MAX_TS_PACKET_LEN-4, tsHeader.continuity_counter);
            }
            else
            {
                printf("0x%08x Transport Packet{PID = 0x%x, Payload = YES(%d), Counter = %d}\n", 
                        offset, tsHeader.pid, MAX_TS_PACKET_LEN-4, tsHeader.continuity_counter);
            }
            
            break;
            
        case 2:
            if (tsHeader.playload_unit_start_indictor)
            {
                printf("0x%08x Transport Packet{PID = 0x%x, Payload = NO, Counter = %d, Start indicactor}\n", 
                        offset, tsHeader.pid, tsHeader.continuity_counter);
            }
            else
            {
                printf("0x%08x Transport Packet{PID = 0x%x, Payload = NO, Counter = %d}\n", 
                        offset, tsHeader.pid, tsHeader.continuity_counter);
            }
            
            break;
            
        case 3:
            if (tsHeader.playload_unit_start_indictor)
            {
                printf("0x%08x Transport Packet{PID = 0x%x, Payload = YES(%d), Counter = %d, Start indicactor}\n", 
                        offset, tsHeader.pid, MAX_TS_PACKET_LEN-4-1-data[4], tsHeader.continuity_counter);
            }
            else
            {
                printf("0x%08x Transport Packet{PID = 0x%x, Payload = YES(%d), Counter = %d}\n", 
                        offset, tsHeader.pid, MAX_TS_PACKET_LEN-4-1-data[4], tsHeader.continuity_counter);
            }
            
            break;
            
        default:
            break;
        
    }
    
    tsPacketNum++;
#endif
}

static void ParseTsPat(unsigned char* const patData, T_TS_PAT *tsPat)
{
    int i = 0;
    int sectionLen = 0;
    
    unsigned char *data = NULL;
    
    T_TS_PAT pat = {0};
    
    memset(&pat, 0x0, sizeof(pat));
    
    data = patData;
    
    pat.table_id = data[0];
    
    if (TS_PAT_TABLE_PID != pat.table_id)
    {
        return;
    }
    
    sectionLen = ((data[1]&0xf)<<8) | data[2];
    pat.section_len = sectionLen;
    
    pat.version_num = ((data[5]>>1) & 0x1f);
    
    data += 8;
    sectionLen -= (5+4); /* len is after section_len and not have crc */
    
    while (sectionLen>0)
    {
        if (i >= MAX_TS_PROGRAM_NUM)
        {
            break;
        }
        
        pat.programs[i].program_number = ((data[0]<<8) | data[1]);
        
        if (0 != pat.programs[i].program_number)
        {
            pat.programs[i].program_map_pid = (((data[2]&0x1f)<<8) | data[3]);
        }
        
        data += 4;
        sectionLen -= 4;
        
        i++;
        
        pat.programNum = i;
    }
    
    memcpy(tsPat, &pat, sizeof(pat));
    
#ifdef PRINTF_DEBUG
    printf("%s%s Program Association Table, version: %d\n", TAB46, TAB44, pat.version_num);
    
    for (i=0; i<pat.programNum; i++)
    {
        printf("%s%s%s program_number: %d, program_map_PID: 0x%x\n", TAB46, TAB44, TAB44, pat.programs[i].program_number, pat.programs[i].program_map_pid);
    }
#endif
}

static void ParseTsPmt(unsigned char* const pmtData, T_TS_PMT *tsPmt)
{
    int i = 0;
    int sectionLen = 0;
    int program_info_length = 0;
    int es_info_length = 0;

    unsigned char *data = NULL;
    
    T_TS_PMT pmt = {0};
    
    memset(&pmt, 0x0, sizeof(pmt));
    
    data = pmtData;
    
    pmt.table_id = data[0];
    
    if (TS_PMT_TABLE_PID != pmt.table_id)
    {
        return;
    }
    
    sectionLen = ((data[1]&0xf)<<8) | data[2];
    pmt.section_len = sectionLen;

    pmt.program_number = data[3]<<8 | data[4];
    
    pmt.version_num = ((data[5]>>1) & 0x1f);
    
    data += 10;
    sectionLen -= (7+4);

    program_info_length = ((data[0]&0xf)<<8) | data[1];

    data += 2;
    sectionLen -= 2;
    
    data += program_info_length;
    sectionLen -= program_info_length;
    
    while (sectionLen > 0)
    {
        if (i >= MAX_TS_STREAM_NUM)
        {
            break;
        }
        
        pmt.streams[i].stream_type = data[0];
        pmt.streams[i].elementary_pid = (((data[1]&0x1f)<<8) | data[2]);
        
        es_info_length = ((data[3]&0xf)<<8) | data[4];

        data += 5;
        sectionLen -= 5;
        
        data += es_info_length;
        sectionLen -= es_info_length;
        
        i++;
        
        pmt.streamNum = i;
    }
    
    pmt.pmtIsFind = 1;
    
    memcpy(tsPmt, &pmt, sizeof(pmt));
    
#ifdef PRINTF_DEBUG
    printf("%s%s Program Map Table, version: %d\n", TAB46, TAB44, pmt.version_num);

    for (i=0; i<pmt.streamNum; i++)
    {
        printf("%s%s%s stream_type: 0x%x(%d), elementary_pid: 0x%x(%d)\n", TAB46, TAB44, TAB44, pmt.streams[i].stream_type, pmt.streams[i].stream_type,
            pmt.streams[i].elementary_pid, pmt.streams[i].elementary_pid);
    }
#endif
}

static void getPdts(unsigned char *pdtsData, long long *pdts, unsigned char *pdtsString)
{
    int hour = 0;
    int minute = 0;
    int second = 0;
    int msecond = 0;
    
    long long pts = 0;
    long long pts2Ms = 0;

    unsigned char ptsStr[MAX_PDTS_STRING_LEN+1] = {0};
    
    /* 5个字节转33位的值 */
    pts = (((pdtsData[0]>>1) & 0x7) << 30) | (pdtsData[1] << 22) | (((pdtsData[2]>>1) & 0x7f) << 15) | (pdtsData[3] << 7) | (pdtsData[4]>>1 & 0x7f);
    
    /* 90KHz, 1000ms/90 */
    pts2Ms = pts/90;
    
    hour = pts2Ms/(60*60*1000);
    minute = (pts2Ms - hour * (60*60*1000)) / (60*1000);
    second = (pts2Ms - hour * (60*60*1000) - minute * (60*1000)) / 1000;
    msecond = pts2Ms - hour * (60*60*1000) - minute * (60*1000) - second * 1000;
    
    sprintf(ptsStr, "%02d:%02d:%02d:%03d", hour, minute, second, msecond);
    
    ptsStr[MAX_PDTS_STRING_LEN] = '\0';
    
    memcpy(pdtsString, ptsStr, MAX_PDTS_STRING_LEN);
    
    *pdts = pts;
}

/**************************************************************************************
1. 根据playload_unit_start_indictor=1, 只解析这个ts包(该字段指示section, pes等的起始标识, 
   若要拼包则根据这个字段, 一个整包结束这个字段会置0);
2. 这里暂时不获取所有的pes包去解析, 只解析PES的头;
3. 可能的问题是一个ts包可能有填充字段, 根据此字段的指示adaptation_field_control(判断有无填充),
   然后4字节包头后的第5个字节指示填充字段的长度, 若该长度大于一个ts包的长度, 则在此处是解析
   不到PES的数据的, 真正的PES数据应该在下一包;
4. adaptation_field_control(适配域控制): 表示包头是否有调整字段或有效负载.
    '00'为ISO/IEC未来使用保留;
    '01'仅含有效载荷, 无调整字段;
    '10'无有效载荷, 仅含调整字段;
    '11'调整字段后为有效载荷, 调整字段中的前一个字节表示调整字段的长度length, 有效载荷开始的位置应再偏移length个字节;
    空包应为'10'.
    ** 这个地方有一个未证实的(基本不会错), 记录下来: adapt=1时, 貌似对于PSI/SI数据在第5个字节会写一个00(代码处理的地方为main(),ParseTsPat(&tsPacket[4+1], &g_TsPat)), 
       对于pes数据若为1, 直接跟有效数据(代码处理的地方为ParsePes(), data += (4+1+data[4]).
5. 此处还有一个需说明的, 有时会发现packet_length为0. 这个比较特殊, 标准里有说明.
   因为pes_packet_length为16个字节, 最大只能支持到65535, 当一个pes包大于这个数值的时候,
   处理方法是这个值为0, 实际的大小由上层协议决定.(只有当ts传输pes的时候才能这么做,
   通过根据playload_unit_start_indictor=1/0就可以确定这个pes包的真实长度.)
6. 对于H264, 可能的就是将SPS, PPS等信息(数据量比较小)放到一个ts包中.
***************************************************************************************/
static void ParsePes(unsigned char* const pesData, T_TS_PACKET_HEADER* const tsHeader)
{
    unsigned char pts_dts_flag;
    
    unsigned char *data = NULL;
    
    unsigned char pts[MAX_PDTS_LEN+1] = {0};
    unsigned char dts[MAX_PDTS_LEN+1] = {0};
    
    T_TS_PES pes = {0};
    
    data = pesData;
    
    memset(&pes, 0x0, sizeof(pes));
    
    /* deal adaptation */
    if ((0 == tsHeader->adaptation_field_control)
        || (2 == tsHeader->adaptation_field_control))
    {
        return;
    }
    
    if (1 == tsHeader->adaptation_field_control)
    {
        data += 4;
    }
    else /* 3 */
    {
        /* header(4) + adaptlen(1) + adaptdata(adaptlen) */
        data += (4+1+data[4]);
    }

    
    data += 3; /* start code 00 00 01*/
    
    pes.streamId = data[0];
    pes.pesLength = (data[1]<<8) | data[2];
    
    data += 3; /* streamid(1) + pes_len(2) */
    
    pts_dts_flag = data[1]>>6 & 0x3;
    
    pes.pesHeaderLen = data[2];
    
    data += 3;
    
    switch (pts_dts_flag)
    {
        case 0: /* 00, no pts, dts */
            break;

        case 2: /* 10, only pts*/
            memset(pts, 0x0, sizeof(pts));

            memcpy(pts, data, MAX_PDTS_LEN);

            getPdts(pts, &pes.pts, pes.ptsStr);

            break;

        case 3: /* 11 pts & dts*/
            memset(pts, 0x0, sizeof(pts));
            memset(dts, 0x0, sizeof(dts));

            memcpy(pts, data, MAX_PDTS_LEN);
            memcpy(dts, data+MAX_PDTS_LEN, MAX_PDTS_LEN);

            getPdts(pts, &pes.pts, pes.ptsStr);
            getPdts(dts, &pes.dts, pes.dtsStr);

            break;

        default:
            break;    
    }

#ifdef PRINTF_DEBUG
    if ((pes.streamId>=0xC0) && (pes.streamId<=0xDF))
    {
        printf("%s%s PES Packet(Audio) {stream_id = 0x%x}\n", TAB46, TAB44, pes.streamId);
    }

    if ((pes.streamId>=0xE0) && (pes.streamId<=0xEF))
    {
        printf("%s%s PES Packet(Video) {stream_id = 0x%x}\n", TAB46, TAB44, pes.streamId);
    }
    
    printf("%s%s%s packet_length = %d, PES_header_data_length = %d\n", TAB46, TAB44, TAB44, pes.pesLength, pes.pesHeaderLen);
    printf("%s%s%s PTS: %s(%lld), DTS: %s(%lld)\n", TAB46, TAB44, TAB46, pes.ptsStr, pes.pts, pes.dtsStr, pes.dts);
#endif

    /*
      1. todo: this test video is h264, parse pes data;
      2. get pes data, find h264 startcode, parse nual.
    */
}

int main(int argc, char *argv[])
{
    int i = 0;
    int j = 0;
    int k = 0;
    int patIsFind = 0;
    int allPmtIsFind = 0;
    
    unsigned char tsPacket[MAX_TS_PACKET_LEN] = {0};
    
    T_TS_PACKET_HEADER tsPacketHeader = {0};

    FILE *fp = NULL;

    if (2 != argc)
    {
        printf("Usage: tsparse **.ts\n");

        return -1;
    }

    memset(&g_TsPat, 0x0, sizeof(T_TS_PAT));
    memset(g_TsPmt, 0x0, sizeof(g_TsPmt));
    
    fp = fopen(argv[1], "rb");
    if (!fp)
    {
        printf("open file[%s] error!\n", argv[1]);

        return -1;
    }
    
    while (1)
    {
        memset(tsPacket, 0x0, MAX_TS_PACKET_LEN);
        memset(&tsPacketHeader, 0x0, sizeof(tsPacketHeader));
        
        if (MAX_TS_PACKET_LEN != fread(tsPacket, 1, MAX_TS_PACKET_LEN, fp))
        {
            break;
        }
        
        ParseTsHeader(tsPacket, &tsPacketHeader);
        
        /* pat->pmt->(audio/video pid)->video data */
        if (0 == patIsFind)
        {
            if (TS_PAT_PID == tsPacketHeader.pid)
            {
                /* 4(header) + 1(adapt len)*/
                ParseTsPat(&tsPacket[4+1], &g_TsPat);
                
                patIsFind = 1;
            }
        }

        if ((1 == patIsFind) && (1 != allPmtIsFind))
        {
            for (i=0; i<g_TsPat.programNum; i++)
            {
                if ((g_TsPat.programs[i].program_map_pid == tsPacketHeader.pid)
                    && (0 == g_TsPmt[j].pmtIsFind))
                {
                    ParseTsPmt(&tsPacket[4+1], &g_TsPmt[j]);
                    
                    j++;
                }
            }
            
            for (i=0; i<g_TsPat.programNum; i++)
            {
                if (0 == g_TsPmt[i].pmtIsFind)
                {
                    break;
                }
            }
            
            if (i == g_TsPat.programNum)
            {
                allPmtIsFind = 1;
            }
        }
        
        if (allPmtIsFind)
        {
            for (i=0; i<g_TsPat.programNum; i++)
            {
                for (k=0; k<g_TsPmt[i].streamNum; k++)
                {
                    if ((g_TsPmt[i].streams[k].elementary_pid == tsPacketHeader.pid)
                        && (1 == tsPacketHeader.playload_unit_start_indictor))
                    {
                        ParsePes(tsPacket, &tsPacketHeader);
                    }
                }
            }
        }
    }
    
    fclose(fp);
}